﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
using System.Text;

namespace Sealight.UI.WebUI.UIHelpers
{
    public class CommonHelper
    {
        public static string GetPageListViewForFronEnd(int currentPage, int totalPage, string url)
        {
            string htmlPage = string.Empty;
            int firstPage = 0, lastPage = totalPage;
            const int displayNum = 5;

            if (totalPage > 0 && currentPage <= totalPage)
            {
                if (currentPage < displayNum)
                {
                    if (totalPage - (displayNum * 2) > 0)
                    {
                        lastPage = displayNum * 2;
                    }
                    else
                    {
                        lastPage = totalPage;
                    }
                }
                else if (totalPage - 1 - currentPage < displayNum)
                {
                    if ((totalPage - 2 * displayNum) > 0)
                    {
                        firstPage = totalPage - 2 * displayNum;
                    }
                    else
                    {
                        firstPage = 0;
                    }
                }
                else
                {
                    if (currentPage - displayNum > 0)
                    {
                        firstPage = currentPage - displayNum;
                    }

                    if (totalPage - (currentPage + displayNum + 1) >= 0)
                    {
                        lastPage = currentPage + displayNum;
                    }
                }

                if (firstPage > 0)
                {
                    htmlPage += string.Format("<a href='{0}?currentPage={1}' class='numberpage-link'><span>...</span></a>", url, firstPage - 1);
                }


                for (int i = firstPage; i < lastPage; i++)
                {
                    if (i == currentPage)
                    {
                        htmlPage += string.Format("<b><span>[{0}]</span></b>", i + 1);
                    }
                    else
                    {
                        htmlPage += string.Format("<a href='{0}?currentPage={1}' class='numberpage-link'><span>{2}</span></a>", url, i, i + 1);

                    }

                }

                if (lastPage < totalPage)
                {
                    htmlPage += string.Format("<a href='{0}?currentPage={1}' class='numberpage-link'><span>...</span></a>", url, lastPage);
                }

            }

            return htmlPage;
        }
        public static string GetPageListView(int currentPage, int totalPage)
        {
            string htmlPage = string.Empty;
            int firstPage = 0, lastPage = totalPage;
            const int displayNum = 5;

            if (totalPage > 0 && currentPage <= totalPage)
            {
                if (currentPage < displayNum)
                {
                    if (totalPage - (displayNum * 2) > 0)
                    {
                        lastPage = displayNum * 2;
                    }
                    else
                    {
                        lastPage = totalPage;
                    }
                }
                else if (totalPage - 1 - currentPage < displayNum)
                {
                    if ((totalPage - 2 * displayNum) > 0)
                    {
                        firstPage = totalPage - 2 * displayNum;
                    }
                    else
                    {
                        firstPage = 0;
                    }
                }
                else
                {
                    if (currentPage - displayNum > 0)
                    {
                        firstPage = currentPage - displayNum;
                    }

                    if (totalPage - (currentPage + displayNum + 1) >= 0)
                    {
                        lastPage = currentPage + displayNum;
                    }
                }

                if (firstPage > 0)
                {
                    htmlPage += string.Format("<a href='javascript:void(0)' class='listview-page-number' rel='{0}'><span>...</span></a>", firstPage - 1);
                }


                for (int i = firstPage; i < lastPage; i++)
                {
                    if (i == currentPage)
                    {
                        htmlPage += string.Format("<b><span>[{0}]</span></b>", i + 1);
                    }
                    else
                    {
                        htmlPage += string.Format("<a href='javascript:void(0)' class='listview-page-number' rel='{0}'><span>{1}</span></a>", i, i + 1);

                    }

                }

                if (lastPage < totalPage)
                {
                    htmlPage += string.Format("<a href='javascript:void(0)' class='listview-page-number' rel='{0}'><span>...</span></a>", lastPage);
                }

            }

            return htmlPage;
        }
        public static string GetPageListViewForFronEnd(int currentPage, int totalPage, string url, IDictionary<string, string> parameters)
        {
            string htmlPage = string.Empty;
            int firstPage = 0, lastPage = totalPage;
            const int displayNum = 5;

            var queryBuilder = new StringBuilder();
            if (parameters != null)
            {
                // Build the query
                foreach (var kvp in parameters)
                {
                    queryBuilder.Append(kvp.Key);
                    queryBuilder.Append("=");
                    queryBuilder.Append(System.Web.HttpUtility.UrlEncode(kvp.Value));
                    queryBuilder.Append("&");
                }
                queryBuilder.Remove(queryBuilder.Length - 1, 1);
            }
            if (totalPage > 0 && currentPage <= totalPage)
            {
                if (currentPage < displayNum)
                {
                    if (totalPage - (displayNum * 2) > 0)
                    {
                        lastPage = displayNum * 2;
                    }
                    else
                    {
                        lastPage = totalPage;
                    }
                }
                else if (totalPage - 1 - currentPage < displayNum)
                {
                    if ((totalPage - 2 * displayNum) > 0)
                    {
                        firstPage = totalPage - 2 * displayNum;
                    }
                    else
                    {
                        firstPage = 0;
                    }
                }
                else
                {
                    if (currentPage - displayNum > 0)
                    {
                        firstPage = currentPage - displayNum;
                    }

                    if (totalPage - (currentPage + displayNum + 1) >= 0)
                    {
                        lastPage = currentPage + displayNum;
                    }
                }

                if (firstPage > 0)
                {
                    htmlPage += string.Format("<a href='{0}?currentPage={1}&{2}' class='numberpage-link'><span>...</span></a>", url, firstPage - 1, queryBuilder.ToString());
                }


                for (int i = firstPage; i < lastPage; i++)
                {
                    if (i == currentPage)
                    {
                        htmlPage += string.Format("<b><span>[{0}]</span></b>", i + 1);
                    }
                    else
                    {
                        htmlPage += string.Format("<a href='{0}?currentPage={1}&{3}' class='numberpage-link'><span>{2}</span></a>", url, i, i + 1, queryBuilder.ToString());

                    }

                }

                if (lastPage < totalPage)
                {
                    htmlPage += string.Format("<a href='{0}?currentPage={1}&{2}' class='numberpage-link'><span>...</span></a>", url, lastPage, queryBuilder.ToString());
                }

            }

            return htmlPage;
        }
        
        public static bool CheckExt(string ext)
        {
            bool fileOK = false;
            string[] allowedExtensions = { ".doc", ".docx", ".pdf", ".excel" };

            for (int i = 0; i < allowedExtensions.Length; i++)
                if (ext == allowedExtensions[i])
                    fileOK = true;

            return fileOK;
        }
        public static string ParseBodyToWord(string postBody, int numberWord, bool appendDots)
        {
            string returnString = "";
            if (postBody != null && postBody.Trim().Length > 0)
            {
                string[] stringSeparators = new string[] { " " };
                string RegExStr = "<[^>]*>";
                Regex R = new Regex(RegExStr);

                postBody = R.Replace(postBody, " ");
                string[] ListString = postBody.Split(stringSeparators, StringSplitOptions.None);
                for (int i = 0; i < Math.Min(numberWord, ListString.Length); i++)
                {
                    returnString += ListString[i] + " ";
                }
                if (appendDots)
                {
                    returnString += (numberWord < ListString.Length) ? "..." : "";
                }
            }
            return returnString;
        }

        public static string ParseBodyToWord(string postBody, int numberWord)
        {
            return ParseBodyToWord(postBody, numberWord, false);
        }

        public static string ParseBodyToCharacters(string postBody, int length)
        {
            string returnString = "";
            if (postBody != null && postBody.Trim().Length > 0)
            {
                var regex = new Regex("(\\<script(.+?)\\</script\\>)|(\\<style(.+?)\\</style\\>)", RegexOptions.Singleline | RegexOptions.IgnoreCase);
                postBody = regex.Replace(postBody, string.Empty);

                //Remove all tag html
                string RegExStr = @"</?\S+?((\s+[\S-[=]]+?(\s*=\s*(?:"".*?""|'.*?'|[^'"">\s]+))?)+\s*|\s*)/?>";

                Regex R = new Regex(RegExStr);
                postBody = R.Replace(postBody, string.Empty);
                postBody = postBody.Replace("&nbsp;", " ");

                RegExStr = @"<![\s\S]*?--[\s]*>";
                R = new Regex(RegExStr);
                postBody = R.Replace(postBody, string.Empty);

                RegExStr = @"\s+?(\S+)?$/g";
                R = new Regex(RegExStr);
                postBody = postBody.Substring(0, Math.Min(length, postBody.Length));

                returnString = R.Replace(postBody, "") + "...";

            }
            return returnString.Trim();
        }
    }
}